package com.symaster.common.swing.element.table;

import com.symaster.common.swing.element.CardListPanel;
import com.symaster.common.swing.style.Style;
import com.symaster.common.swing.util.SwingUtil;

import javax.swing.*;
import java.awt.*;
import java.util.Arrays;

/**
 * @author yinmiao
 * @version 2022-05-10 18:51
 */
public class TableList extends JPanel {
    public static final Color SPLIT_LINE_COLOR = new Color(191, 191, 191);
    private final int width;
    private JPanel root_panel;
    private JPanel head_panel;
    private JPanel center_list_panel;
    private CardListPanel cardListPanel;
    private final ColWidth colWidth;

    public static void main(String[] args) {
        TableList tableList = new TableList(500, new Head("id", "name", "col2", "col3", "col4"), new ColWidth(20, 40, -1, -1, -1));
        tableList.addRow(new Row(new JLabel("1"), new JLabel("尹淼"), new JLabel("-"), new JLabel("-"), new JLabel("-")));
        tableList.addRow(new Row(new JLabel("2"), new JLabel("尹淼2"), new JLabel("-"), new JLabel("-"), new JLabel("-")));
        tableList.addRow(new Row(new JLabel("3"), new JLabel("尹淼3"), new JLabel("-"), new JLabel("-"), new JLabel("-")));

        JFrame jFrame = new JFrame();
        jFrame.setContentPane(tableList);
        jFrame.setPreferredSize(new Dimension(500, 500));
        jFrame.pack();
        SwingUtil.toCenter(jFrame);
        jFrame.setVisible(true);
    }

    public TableList(int width, Head head) {
        this.width = width;
        initPage();

        String[] names = head.getNames();
        if (names == null || names.length <= 0) {
            throw new NullPointerException();
        }

        JPanel rowItemPanel = new JPanel();
        rowItemPanel.setPreferredSize(new Dimension(width, 30));
        head_panel.add(rowItemPanel);

        int[] colWidthArray = new int[names.length];
        Arrays.fill(colWidthArray, -1);
        colWidth = new ColWidth(colWidthArray);

        rowItemPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));

        Dimension dimension = new Dimension((width / colWidthArray.length) - (colWidthArray.length - 1), 30);
        for (int i = 0; i < names.length; i++) {
            String colName = names[i];
            JLabel jLabel = new JLabel(colName, JLabel.CENTER);
            jLabel.setPreferredSize(dimension);
            rowItemPanel.add(jLabel);
            if (i + 1 < names.length) {
                JPanel jPanel = new JPanel();
                jPanel.setPreferredSize(new Dimension(1, 20));
                jPanel.setBackground(SPLIT_LINE_COLOR);
                rowItemPanel.add(jPanel);
            }
        }
    }

    public TableList(int width, Head head, ColWidth colWidth) {
        this.width = width;
        this.colWidth = colWidth;
        initPage();

        String[] names = head.getNames();
        if (names == null || names.length <= 0) {
            throw new NullPointerException();
        }

        JPanel rowItemPanel = new JPanel();
        rowItemPanel.setPreferredSize(new Dimension(width, 30));
        head_panel.add(rowItemPanel);

        rowItemPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
        int[] colWidths = colWidth.getColWidths();
        int[] availableWidth = getAvailableWidth();

        Dimension dimension = new Dimension((availableWidth[0] / (colWidths.length - availableWidth[1])) - (colWidths.length - 1), 30);

        for (int i = 0; i < names.length; i++) {
            JLabel jLabel = new JLabel(names[i], JLabel.CENTER);
            int colWidth1 = colWidths[i];
            if (colWidth1 < 0) {
                jLabel.setPreferredSize(dimension);
            } else {
                jLabel.setPreferredSize(new Dimension(colWidth1, 30));
            }
            rowItemPanel.add(jLabel);
            if (i + 1 < names.length) {
                JPanel jPanel = new JPanel();
                jPanel.setPreferredSize(new Dimension(1, 20));
                jPanel.setBackground(SPLIT_LINE_COLOR);
                rowItemPanel.add(jPanel);
            }
        }
    }

    private int[] getAvailableWidth() {
        int[] colWidths = colWidth.getColWidths();
        int sum = 0;
        int count = 0;
        for (int i : colWidths) {
            if (i > 0) {
                sum += i;
                count++;
            }
        }

        return new int[]{width - sum, count};
    }

    private void initPage() {
        setLayout(new BorderLayout());
        add(root_panel);
        center_list_panel.add((cardListPanel = new CardListPanel(width, 30, 100, 0)));
    }

    public void addRow(Row row) {
        if (row.components == null || row.components.length <= 0) {
            throw new NullPointerException();
        }

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        JButton buttonLayoutPanel = new JButton();
        buttonLayoutPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
        int[] colWidths = colWidth.getColWidths();
        int[] availableWidth = getAvailableWidth();
        Dimension dimension = new Dimension((availableWidth[0] / (row.components.length - availableWidth[1])) - (colWidths.length - 1), 30);

        for (int i = 0; i < row.components.length; i++) {
            JComponent component = row.components[i];

            int colWidth1 = colWidths[i];
            if (colWidth1 < 0) {
                component.setPreferredSize(dimension);
            } else {
                component.setPreferredSize(new Dimension(colWidth1, 30));
            }
            buttonLayoutPanel.add(component);
            if (i + 1 < row.components.length) {
                JPanel jPanel = new JPanel();
                jPanel.setPreferredSize(new Dimension(1, 15));
                jPanel.setBackground(SPLIT_LINE_COLOR);
                buttonLayoutPanel.add(jPanel);
            }
        }
        Style.setBorder(1, buttonLayoutPanel);
        panel.add(buttonLayoutPanel, BorderLayout.CENTER);

        JPanel splitLine = new JPanel();
        splitLine.setPreferredSize(new Dimension(0, 1));

        panel.add(splitLine, BorderLayout.SOUTH);

        cardListPanel.add(panel);
    }

    public void removeAllRow() {
        cardListPanel.removeAll();
    }

    {
        // GUI initializer generated by IntelliJ IDEA GUI Designer
        // >>> IMPORTANT!! <<<
        // DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        root_panel = new JPanel();
        root_panel.setLayout(new BorderLayout(0, 0));
        head_panel = new JPanel();
        head_panel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
        head_panel.setPreferredSize(new Dimension(0, 30));
        root_panel.add(head_panel, BorderLayout.NORTH);
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new BorderLayout(0, 0));
        root_panel.add(panel1, BorderLayout.CENTER);
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new BorderLayout(0, 0));
        panel2.setBackground(new Color(-4210753));
        panel2.setPreferredSize(new Dimension(0, 2));
        panel1.add(panel2, BorderLayout.NORTH);
        center_list_panel = new JPanel();
        center_list_panel.setLayout(new BorderLayout(0, 0));
        panel1.add(center_list_panel, BorderLayout.CENTER);
    }

    /** @noinspection ALL */
    public JComponent $$$getRootComponent$$$() {
        return root_panel;
    }

    public static class Head {
        private String[] names;

        public Head() {
        }

        public Head(String... names) {
            this.names = names;
        }

        public String[] getNames() {
            return names;
        }

        public void setNames(String[] names) {
            this.names = names;
        }

        @Override
        public String toString() {
            return "Head{" +
                    "names=" + Arrays.toString(names) +
                    '}';
        }
    }

    public static class Row {
        private JComponent[] components;

        public Row(JComponent... components) {
            this.components = components;
        }

        public JComponent[] getComponents() {
            return components;
        }

        public void setComponents(JComponent[] components) {
            this.components = components;
        }

        @Override
        public String toString() {
            return "Row{" +
                    "components=" + Arrays.toString(components) +
                    '}';
        }
    }

    public static class ColWidth {
        private int[] colWidths;

        public ColWidth(int... colWidths) {
            this.colWidths = colWidths;
        }

        public int[] getColWidths() {
            return colWidths;
        }

        public void setColWidths(int[] colWidths) {
            this.colWidths = colWidths;
        }

        @Override
        public String toString() {
            return "ColWidth{" +
                    "colWidths=" + Arrays.toString(colWidths) +
                    '}';
        }
    }
}
